package com.supermap.search.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.supermap.search.R;
import com.supermap.search.adapter.MyAdapter;
import com.supermap.search.javabean.XunJianTask;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class XunJianTaskActivity extends AppCompatActivity {


    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;


    private List<XunJianTask> list;
    private MyAdapter<XunJianTask> adapter;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            swipeLayout.setRefreshing(false);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xun_jian_task);
        ButterKnife.bind(this);
        list = new ArrayList<>();
        init();
    }


    public void init() {
        initToolbar();
        initData();
        initSwipe();

    }

    private void initToolbar() {
        toolbarTitle.setText("巡检任务");
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        XunJianTask bean1 = new XunJianTask("001", "严重", "2018-05-11 00:00:00", "去现场看看空调设备是否运行正常","113.300355","23.399584");
        list.add(bean1);
        if(adapter==null){
            adapter = new MyAdapter<XunJianTask>(list, R.layout.item_lv_xunjiantask){//113.300355,23.399584

                @Override
                public void bindView(ViewHolder holder, XunJianTask obj) {
                    holder.setText(R.id.tv_desc,obj.getDesc());
                    holder.setText(R.id.tv_time,obj.getTime());
                    holder.setText(R.id.tv_status,obj.getStatus());
                    holder.setText(R.id.tv_id,"编号："+obj.getId());
                }
            };
            listview.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(XunJianTaskActivity.this, MainActivity.class);
                intent.putExtra("x",list.get(position).getX());
                intent.putExtra("xunjian",8);
                intent.putExtra("y",list.get(position).getY());
                startActivity(intent);
                finish();
            }
        });

    }

    private void initSwipe() {
        swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
                handler.sendEmptyMessageDelayed(0,2000);

            }
        });


        swipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
//        SwipeLy.setDistanceToTriggerSync(400);// 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeLayout.setSize(SwipeRefreshLayout.DEFAULT); // 设置圆圈的大小
    }
}
